package com.klangner.go.app.rules;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;



// class for a group of stones
/**
 * Represents group of stones with information about it
 */
public class StonesGroupOld {

	/** Stones color */
	private int color;

	/** stones list */
//  private StonesList stones;
  private Vector<GobanLocation>	stones;

  /** group's liberties */
  private Set<GobanLocation> liberties;


  /**
   * constructor; each group has at least one stone 
   * @param stone
   */
  public StonesGroupOld(int color) {

  	this.color = color;
    stones = new Vector<GobanLocation>();

  }

  /**
   * Add stone to the group
   * @param x
   * @param y
   * @param color
   */
  public void add(GobanLocation position){
  	stones.add(position);
  	position.setGroup(this);
    //copy the group's list of liberties from the stone's
    liberties = new HashSet<GobanLocation>(position.getLiberties());
  }
  
  /**
   * Add another group
   * @param x
   * @param y
   * @param color
   */
  public void add(StonesGroupOld group){
  	Iterator<GobanLocation> stones = group.getStones().iterator();
  	
  	while(stones.hasNext()){
  		add(stones.next());
  	}
  }
  
  
  /**
   * @return stones color
   */
  public int getColor(){
  	return color;
  }
  
  /**
   * @return stones
   */
  public Vector<GobanLocation> getStones(){
  	return stones;
  }
  
  
  /**
   * @return group liberties
   */
  public Set<GobanLocation> getLiberties() {
      return liberties;
  }
    
    
    public boolean hasNoLiberty() {
        return (liberties.isEmpty());
    }

    public int nrLiberties() {
        return liberties.size();
    }

        // overriding Object's comparing method
        /** I consider 2 groups to be "equal" if one stone of the first
         * is contained in the second */
    public boolean equals(Object anObject) {
        if (anObject instanceof StonesGroupOld) {
            StonesGroupOld anotherGroup = (StonesGroupOld)anObject;
            if (this.stones.contains(anotherGroup.stones.get(0)))
                return true;
        }
        return false;
    }

    public String toString() {
        return "[" + color + "; " + "S=" + stones + ";L=" + liberties +"]";
    }

    public int nrStones() {
        return stones.size();
    }

    public int color() {
        return color;
    }       

}
